package com.ai.aicontact.busi.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ai.aicontact.busi.entity.ExecLimitCfg;
import com.ai.aicontact.busi.service.ExecLimitCfgService;
import com.ai.aicontact.common.constant.GlobalConstant;
import com.ai.aicontact.common.util.NumberUtils;
import com.ai.aicontact.common.vo.PageFilter;
import com.ai.aicontact.core.dao.BaseDaoI;

@Service
public class ExecLimitCfgServiceImpl implements ExecLimitCfgService{

	 @Autowired
	 BaseDaoI<ExecLimitCfg> execLimitCfgDao;
	
	@Override
	public void saveExecLimitCfgDayLimit(ExecLimitCfg execLimitCfg) throws Exception {
			
		 if (execLimitCfg != null) {
			 if (execLimitCfg.getLimitId() != null) {
				 ExecLimitCfg execLimitCfgFDB = execLimitCfgDao.get(ExecLimitCfg.class, execLimitCfg.getLimitId());
				 execLimitCfgFDB.setLimitValue(execLimitCfg.getLimitValue());
				execLimitCfgDao.update(execLimitCfgFDB);
			 } else {
				 Long targetId = execLimitCfg.getTargetId();
				 ExecLimitCfg execLimitCfgFDB = queryExecLimitCfgDayLimitByTargetId(targetId);
				 if (execLimitCfgFDB != null) {
					 throw new Exception("该渠道/触点已经存在配置数据,不可重复配置");
				 }
				 if(!NumberUtils.isNotNullOrZero(execLimitCfg.getTargetId())){ 
					 throw new Exception("您没有选择限制目标");
				 }
				 execLimitCfg.setType(GlobalConstant.EXEC_LIMIT_CFG_TYPE_DAY_LIMIT);
				 execLimitCfgDao.save(execLimitCfg);
			 }
		 }
	}

	@Override
	public List<ExecLimitCfg> queryExecLimitCfgDayLimit(String keyWord,ExecLimitCfg execLimitCfg, PageFilter pf) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder hql = new StringBuilder("from ExecLimitCfg ");
		hql.append(whereHql(keyWord,execLimitCfg, params));
		hql.append(orderHql(pf)); 
		List<ExecLimitCfg> list = execLimitCfgDao.find(hql.toString(), params, pf.getPageNumber(), pf.getPageSize());
		return list;
	}
	
	
	private String whereHql(String keyWord,ExecLimitCfg execLimitCfg, Map<String, Object> params) {
		StringBuilder hql = new StringBuilder(" where  state=1 AND type= :type ");
		params.put("type", "DAY_LIMIT");
		
		String tagetType = execLimitCfg.getTargetType();
		if(StringUtils.isNotBlank(tagetType)){ 
			hql.append(" AND targetType = :targetType");
			params.put("targetType", tagetType);
		}
		
		Long targetId = execLimitCfg.getTargetId();
		if (targetId != null) {
			hql.append(" AND targetId = :targetId");  //注意如果“:"是中文类型的，就会报param找不到的问题
			params.put("targetId",targetId);
		}
		
		return hql.toString();
	}
	
	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by " + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	@Override
	public Long countExecLimitCfgDayLimit(String keyWord, ExecLimitCfg execLimitCfg) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder hql = new StringBuilder("select count(*) from ExecLimitCfg ");
		hql.append(whereHql(keyWord,execLimitCfg, params));
		Long total = execLimitCfgDao.count(hql.toString(), params);
		return total;
	}

	@Override
	public void deleteExecLimitCfgDayLimit(ExecLimitCfg execLimitCfg) throws Exception {
		
		if (execLimitCfg != null) {
			ExecLimitCfg execLimitCfg2 = execLimitCfgDao.get(ExecLimitCfg.class, execLimitCfg.getLimitId());
			execLimitCfgDao.delete(execLimitCfg2);
		}
	}

	@Override
	public ExecLimitCfg queryExecLimitCfgDayLimitByLimitId(Long limitIdLong) throws Exception {
		return execLimitCfgDao.get(ExecLimitCfg.class, limitIdLong);
	}

	@Override
	public ExecLimitCfg queryExecLimitCfgDayLimitByTargetId(Long targetId) throws Exception {
		ExecLimitCfg execLimitCfg = null;
		
		if (targetId != null) {
			StringBuilder hql = new StringBuilder("from ExecLimitCfg where state=1 AND type= :type AND targetId= :targetId ");
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("targetId", targetId);
			params.put("type", "DAY_LIMIT");
			execLimitCfg = execLimitCfgDao.get(hql.toString(), params);
		}
		return execLimitCfg;
	}

}
